home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2007 December
/
PCWKCD1207B.iso
/
Blogowanie poza sfera
/
Flock 0.9.1.3 stable
/
flock-0.9.1.3.en-US.win32.exe
/
flock
/
components
/
flockLoggingService.js
< prev
next >
Wrap
Text File
|
2007-10-12
|
7KB
|
206 lines
//
// BEGIN FLOCK GPL
//
// Copyright Flock Inc. 2005-2007
// http://flock.com
//
// This file may be used under the terms of of the
// GNU General Public License Version 2 or later (the "GPL"),
// http://www.gnu.org/licenses/gpl.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// END FLOCK GPL
//
const CLASS_ID = Components.ID("{E8C889A6-F832-4B09-A216-66A7E6178896}");
const CLASS_NAME = "Flock Logging Service";
const CONTRACT_ID = "@flock.com/logging-service;1";
const flockILoggingService = Components.interfaces.flockILoggingService;
function flockLoggingService()
{
this.setLevel(flockILoggingService.LEVEL_ALL);
this._observers = new Array();
this._enabled = true;
var catmgr = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
var enum = catmgr.enumerateCategory("flockILoggingObserver");
while(enum.hasMoreElements()) {
try {
var obj = enum.getNext();
var supportsString = obj.QueryInterface(Components.interfaces.nsISupportsCString);
var shortName = supportsString.toString();
var cid = catmgr.getCategoryEntry("flockILoggingObserver", shortName);
var svc = Components.classes[cid].getService(Components.interfaces.flockILoggingObserver);
this.addObserver(svc);
} catch(e) {}
}
var obs = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
obs.addObserver(this, "profile-after-change", false);
}
flockLoggingService.prototype = {
_observers: null,
_level: null,
_enabled: null,
log: function(aLevel, aContext, aMessage)
{
if (this._enabled) {
if (aLevel >= this._level) {
var date = new Date();
for(var i = 0; i < this._observers.length; i++) {
this._observers[i].emit(date.getTime(), aLevel, aContext, aMessage);
}
}
}
},
setLevel: function(aLevel) {
this._level = aLevel;
},
addObserver: function(aObserver)
{
if (aObserver == null) {
throw Components.Exception("Observer is null!!");
}
this._observers.push(aObserver);
},
removeObserver: function(aObserver)
{
for(var i = 0; i < this._observers.length; i++) {
if(aObserver == this._observers[i]) {
this._observers.splice(i,1);
break;
}
}
},
// nsIObserver
observe: function(subject, topic, state)
{
switch (topic) {
case "profile-after-change":
var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2);
// watch prefs for changes
prefService.addObserver("flock.service.logger.enabled", this, false);
prefService.addObserver("flock.service.logger.level", this, false);
// init with current prefs
this.observe(null, "nsPref:changed", null);
break;
case "nsPref:changed":
var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
if (prefService.getPrefType("flock.service.logger.enabled")) {
this._enabled = prefService.getBoolPref("flock.service.logger.enabled");
} else {
this._enabled = true;
}
if (prefService.getPrefType("flock.service.logger.level")) {
this.setLevel(prefService.getIntPref("flock.service.logger.level"));
} else {
this.setLevel(flockILoggingService.LEVEL_WARN);
}
break;
}
},
// nsIClassInfo
getInterfaces: function(aCount)
{
var interfaces = [Components.interfaces.flockILoggingService, Components.interfaces.nsIClassInfo, Components.interfaces.nsIObserver];
aCount.value = interfaces.length;
return interfaces;
},
// nsIClassInfo
getHelperForLanguage: function(aLanguage)
{
return null;
},
// nsIClassInfo
contractID: CONTRACT_ID,
// nsIClassInfo
classDescription: CLASS_NAME,
// nsIClassInfo
classID: CLASS_ID,
// nsIClassInfo
implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
// nsIClassInfo
flags: Components.interfaces.nsIClassInfo.SINGLETON,
// nsISupports
QueryInterface: function(aIID)
{
if (!aIID.equals(Components.interfaces.nsISupports) && !aIID.equals(Components.interfaces.flockILoggingService) && !aIID.equals(Components.interfaces.nsIClassInfo) && !aIID.equals(Components.interfaces.nsIObserver))
throw Components.results.NS_ERROR_NO_INTERFACE;
return this;
}
};
/******************************************************************************
* XPCOM Functions for construction and registration
******************************************************************************/
var Module = {
_firstTime: true,
registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
{
if (this._firstTime) {
this._firstTime = false;
throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
}
aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
// Make the Logging Service a startup observer
var categoryManager = Components.classes["@mozilla.org/categorymanager;1"]
.getService(Components.interfaces.nsICategoryManager);
categoryManager.addCategoryEntry("app-startup", CLASS_NAME, "service," + CONTRACT_ID, true, true);
},
unregisterSelf: function(aCompMgr, aLocation, aType)
{
aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);
},
getClassObject: function(aCompMgr, aCID, aIID)
{
if (!aIID.equals(Components.interfaces.nsIFactory))
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
if (aCID.equals(CLASS_ID))
return Factory;
throw Components.results.NS_ERROR_NO_INTERFACE;
},
canUnload: function(aCompMgr) { return true; }
};
var Factory = {
createInstance: function(aOuter, aIID)
{
if (aOuter != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
return (new flockLoggingService()).QueryInterface(aIID);
}
};
function NSGetModule(aCompMgr, aFileSpec) { return Module; }